import pathlib
import time
import re
import typing
import os
import yaml
import ctypes
import psutil
import subprocess
import requests
import winsound
import datetime
from selenium.webdriver.edge.webdriver import WebDriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import *

# ╔════════════════════════║
# ║                  全局变量存储区               ║
# ║  用途：存放跨模块共享的变量、配置与状态信息   ║
# ║  规范：所有变量以g_为前缀，按功能分组管理     ║
# ╚════════════════════════║
g_Title_Xpath = "//input[@id='title']"



def CheckTitleElementExist(driver:WebDriver,Check_Time:float) -> bool:
    """检查当前页面是否存在可以输入的标题元素

    Args:
        driver (WebDriver): selenium的WebDriver对象
        Check_Time (float): 检查持续时间

    Returns:
        bool: 存在标题元素返回True，否则返回False
    """
    
    start_check_time = datetime.dateime.now()
    while start_check_time + datetime.timedelta(seconds=Check_Time) > datetime.datetime.now():
        try:
            element = driver.find_element(by=By.XPATH,value=g_Title_Xpath)
            return True
        except NoSuchElementException:
            return False



def SetTitle(driver:WebDriver,Title:str) -> None:
    """设置标题

    Args:
        driver (WebDriver): selenium的WebDriver对象
        Title (str): 标题内容
    """
    
    # 1.查找标题元素
    if CheckTitleElementExist(driver,5):
        title_element = driver.find_element(by=By.XPATH,value=g_Title_Xpath)
        
        try:
            title_element.send_keys(Title)
        except:
            pass
    else:
        return